import 'package:flutter/cupertino.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';
import 'package:my_app/base/foundation/jd_cache.dart';
import 'package:my_app/base/utils/jd_log.dart';

TDLoginChangeNotifier kLoginChangeNotifier = TDLoginChangeNotifier();

class TDLoginChangeNotifier extends ChangeNotifier {
  loginStateChange() {
    notifyListeners();
  }
}

class LoginTool {
  LoginTool._();
  static LoginTool? _instance;
  static LoginTool get instance => _getOrCreateInstance();
  static LoginTool _getOrCreateInstance() {
    if (_instance != null) {
      return _instance!;
    } else {
      _instance = LoginTool._();
      return _instance!;
    }
  }

  static String get getUserId {
    return LoginTool.instance.userId;
  }

  String get userId {
    return JDCache.getString('userid') ?? '';
  }

  static bool isNotLogin() {
    return LoginTool.instance.userId.convertInt() <= 0;
  }

  static bool isLogin() {
    return LoginTool.instance.userId.convertInt() > 0;
  }

  static loginIn(Map<String, dynamic> loginMap) {
    JDCache.setString('userid', '1001');
  }

  static loginOut() {
    jdLog('loginOut------>');
    JDCache.remove('userid');
    JDCache.deleteCacheWithPrefix(kNeedLoginOutCleanPrefix);
    kLoginChangeNotifier.loginStateChange();
  }
}
